﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>
int main()
{
	char arr[] = "Hello World";

	//因为字符串放在数组中他是有对应下标的，0也算一个
	//将字符串中的字符逐个进行处理
	//遇到小写，转换成大写，再输出
	//如果不是小写，正常输出
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]))
		{
			arr[i] = arr[i] - 32;
		}
		printf("%c", arr[i]);
		i++;
	}
	return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
	int arr[] = "Hello World";
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]))
		{
			arr[i] = arr[i] - 32;
		}
		printf("%c", arr[i]);
		i++;
	}
	return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
	char arr[] = "Hello World";
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]))
		{
			arr[i] = toupper(arr[i]);
		}
		printf("%c", arr[i]);
		i++;
	}
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	const char* str = "abcdef";
	printf("%zu\n", strlen(str));
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	if (strlen(str2) - strlen(str1) > 0)
	{
		printf("str2 > str1\n");
	}
	else
	{
		printf("str1 > str2\n");
	}
	return 0;
}​
#include<stdio.h>
#include<string.h>
int main()
{
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	if ((int)strlen(str2) - (int)strlen(str1) > 0)
	{
		printf("str2 > str1\n");
	}
	else
	{
		printf("str1 > str2\n");
	}
	return 0;
}
//计数器⽅式 
#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{
	int count = 0;
	assert(str);
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char arr[] = "Hello World";
	int ret = my_strlen(arr);
	printf("%d", ret);
	return 0;
}
#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{
	int count = 0;
	assert(str);
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	int arr[] = "Hello World";
	int ret = my_strlen(arr);
	printf("%d", ret);
	return 0;
}
#include<stdio.h>
size_t my_strlen(const char* str)
{
	if (*str != '\0')
		return 1 + my_strlen(str + 1);
	else
		return 0;
}
int main()
{
	char arr[] = "Hello World";
	size_t len = my_strlen(arr);
	printf("%zu\n", len);
	return 0;
}
#include<stdio.h>
#include<assert.h>
size_t my_strlen(const char* str)
{
	assert(str);
	char* p = str;
	while (*p != '\0')
		p++;
	return p - str;
}
int main()
{
	char arr[] = "Hello World";
	size_t len = my_strlen(arr);
	printf("%zu\n", len);
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[20] = { 0 };
	char arr2[] = "hello world";
	//我们可能会想到不用这个函数直接使用循环的方式
	//arr1 = arr2 (这是不能使用的)
	//因为数组名是地址，地址是一个编号
	//地址是一个常量值，不能被修改
	//地址是指向空间的，但是地址不是空间
	char* ret = strcpy(arr1, arr2);
	printf("%s\n", ret);
	return 0;
}
#include<stdio.h>
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	//拷贝\0前面的字符
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;//拷贝\0
	return ret;
}
int main()
{
	char arr1[] = "hello world";
	char arr2[20] = "******************";
	my_strcpy(arr2, arr1);
	printf("%s\n", arr2);
}
#include <stdio.h>
#include <assert.h>
//1.参数顺序 
//2.函数的功能，停⽌条件 
//3.assert
//4.const修饰指针 
//5.函数返回值 
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);

	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[10] = { 0 };
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);

	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[20] = "hello";
	char arr2[] = " world";
	strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	//找到目标空间中的第一个\0
	while (*dest)
	{
		dest++;
	}
	//从\0后面的位置开始追加
	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "hello";
	char arr2[] = " world";
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abq";
	int ret = strcmp(arr1, arr2);
	printf("%d\n", ret);
	if (ret > 0)
		printf("arr1 > arr2\n");
	else if (ret == 0)
		printf("arr1 == arr2\n");
	else
		printf("arr1 < arr2\n");
	return 0;
}
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* arr1, const char* arr2)
{
	int ret = 0;
	assert(arr1 != NULL);
	assert(arr2 != NULL);
	while (*arr1 == *arr2)
	{
		if (*arr1 == '\0')
			return 0;
		arr1++;
		arr2++;
	}
	return *arr1 - *arr2;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abq";
	int ret = my_strcmp(arr1, arr2);
	if (ret > 0)
		printf("arr1 > arr2\n");
	else if (ret == 0)
		printf("arr1 = arr2\n");
	else
		printf("arr1 < arr2\n");
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[20] = { 0 };
	char arr2[] = "abcdefghi";
	char* str = strncpy(arr1, arr2, 5);
	//将arr2中的前五个字符拷贝到arr1中，如果不够会补\0
	printf("%s\n", arr1);
	printf("%s\n", str);
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "hello";
	char arr2[] = " world";
	char* str = strncat(arr1, arr2, 5);
	//我们追加的时候是从第一个\0开始追加的
	//追加完成之后会补\0
	//如果追加到字符数超过了原有字符数
	//只要追加上原来的即可
	printf("%s\n", arr1);
	printf("%s\n", str);
	return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcqw";
	int ret1 = strncmp(arr1, arr2, 3);
	printf("%d\n", ret1);
	int ret2 = strncmp(arr1, arr2, 4);
	printf("%d\n", ret2);
	return 0;
}